import { createRouter, createWebHistory } from 'vue-router'
// import store from '../store/index'
import { setTitle } from '../utils/index'

const routes = [
  { path: '/', redirect: '/home' },
  {
    path: '/home',
    component: () => import('../views/tabbar/index.vue'),
    children: [
      {
        path: '',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页'
        }
      },
      {
        path: 'classification',
        component: () => import('../views/tabbar/classification.vue'),
        meta: {
          title: ' 分类'
        }
      },
      {
        path: 'cart',
        component: () => import('../views/tabbar/cart.vue'),
        meta: {
          title: '购物车'
        }
      },
      {
        path: 'user',
        component: () => import('../views/tabbar/user.vue'),
        meta: {
          title: '我的'
        }
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
  routes
})

router.beforeEach((to, from, next) => {
  setTitle(to.meta.title)
  // if (to.path == '/index' || to.path == '*') {
  next()
  // } else {
  //   if (store.state.token) {
  //     next()
  //   } else {
  //     store.commit('signOut')
  //     next({ path: '/index' })
  //   }
  // }
})

export default router
